<?php

/**
 * Remote translations
 *
 * @author Highstrike (highstrike@gmail.com)
 * @package CargoClaims
 * @version 1.0.dev
 */
class App_Model_Remote extends MVC_Model
{
	public static $languages = array(
		'English' => array(
			'source' => 'http://www.cargojobs.nl/languages/en.xml',
			'to' => 'en.xml'
		),
		'Nederlands' => array(
			'source' => 'http://www.cargojobs.nl/languages/nl.xml',
			'to' => 'nl.xml'
		)
	);

	/**
	 * Generate the data and return it
	 * @param string $lang The language
	 */
	public function generate($lang)
	{
		// save to cache
		$data = simplexml_load_file(self::$languages[$lang]['source']);
		$data->asXML(ROOT. DS. 'tmp'. DS. self::$languages[$lang]['to']);

		// return data
		return $data;
	}

	/**
	 * get the data from cache or generate it
	 * @param string $lang The language
	 */
	public function data($lang)
	{
		// get cache or generate if cache is 1 hour old
		$cache = @simplexml_load_file(ROOT. DS. 'tmp'. DS. self::$languages[$lang]['to']);
		if($cache)
			return filemtime(ROOT. DS. 'tmp'. DS. self::$languages[$lang]['to']) <= time() - 3600 ? $this->generate($lang) : $cache;

		// generate cache
		return $this->generate($lang);
	}
}